/****************************************************************************
 * Copyright (C) 2012 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "SpiffyListTask.h"
#include "../../Controls/Application.h"
#include "../../Network/networkops.h"

SpiffyListTask::SpiffyListTask(const std::string &ListUrl)
	: url(ListUrl)
{
	running = true;
	list = NULL;
	
	Prompt = new ThrobberWindow(tr("Downloading pagelist:"), "www.spiffy360.com", tr("Cancel"));
	Prompt->DimBackground(true);
	Application::Instance()->Append(Prompt);
	Application::Instance()->SetUpdateOnly(Prompt);
	
	ThreadedTaskHandler::Instance()->AddTask(this);
}

void SpiffyListTask::CloseProgress()
{
	Prompt->SetEffect(EFFECT_SLIDE_TOP | EFFECT_SLIDE_OUT, 50);
	while(Prompt->IsAnimated())
		usleep(50);
	
	Application::Instance()->PushForDelete(Prompt);
}

void SpiffyListTask::TaskError(const char * msg)
{
	if(list)
		delete list;
	list = NULL;
	
	ShowError(msg);
	
	running = false;
}

void SpiffyListTask::Execute(void)
{
	if(!CheckConnection(url.c_str()))
	{
		CloseProgress();
		TaskError(tr("Connection to server timed out."));
		return;
	}

	list = new Theme_List(url.c_str());

	if (!list->GetThemeCount())
	{
		CloseProgress();
		TaskError(tr("No themes found on the site."));
		return;
	}
	
	
	CloseProgress();

	running = false;
}
